using System.Collections;
using UnityEngine;

public class FruitTree : MonoBehaviour, IFruitController
{
	[Header("State")]
	public byte amountOfFruits;

	[Header("Requirement")]
	public GameObject fruit;

	private bool isGrowingFruits;

	private Vector3 position;

	private void Start()
	{
		position = base.transform.position;
		StartCoroutine(GrowFruit());
	}

	private IEnumerator GrowFruit()
	{
		isGrowingFruits = true;
		do
		{
			CreateFruit();
			yield return Engine.growingTime;
		}
		while (++amountOfFruits < 2);
		isGrowingFruits = false;
	}

	private void CreateFruit()
	{
		GameObject gameObject = Object.Instantiate(fruit);
		Vector2Int point = Map.FindNearlestEmptyPoint(position + Main.RandomVector3(2), GridType.Soil);
		gameObject.GetComponent<Fruit>().Begin(this, point);
	}

	public void EatFruit()
	{
		amountOfFruits--;
		if (!isGrowingFruits)
		{
			StartCoroutine(GrowFruit());
		}
	}
}
